import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '@/views/Login'
import Home from '@/views/Home'
import Layout from '@/views/Layout'
// import User from '@/views/User'
// import UserEditor from '@/views/User/UserEditor.vue'
// import Search from '@/views/Search'
// import SearchResult from '@/views/Search/SearchResult.vue'
// import ArticleDetail from '@/views/ArticleDetail'
// import Chat from '@/views/Chat'
import store from '@/store'

Vue.use(VueRouter)

const routes = [
  // 规则
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    // component: Login
    component: () => import(/* webpackChunkName: "Login" */ '@/views/Login'),
    // 路由独享守卫
    beforeEnter: (to, from, next) => {
      // ..需求: 登录状态下,还想去登录页面,留在原地不能跳转
      if (store.state.token.length) {
        next(false) // 原地留下
      } else {
        next() // 其他情况放行
      }
    }
  },
  {
    path: '/layout',
    component: Layout,
    name: 'Layout',
    redirect: '/layout/home',
    children: [
      {
        path: 'home',
        component: Home
      },
      {
        path: 'user',
        component: () => import(/* webpackChunkName: "User" */ '@/views/User')
      }
    ]
  },
  {
    path: '/search',
    component: () => import(/* webpackChunkName: "Search" */ '@/views/Search')
  },
  {
    path: '/search/:keywords',
    component: () => import(/* webpackChunkName: "SearchResult" */ '@/views/Search/SearchResult.vue')
  },
  {
    // 需要传参，准备用 ？ 号传参
    path: '/article',
    component: () => import(/* webpackChunkName: "ArticleDetail" */ '@/views/ArticleDetail')
  },
  {
    path: '/user/editor',
    component: () => import(/* webpackChunkName: "UserEditor" */ '@/views/User/UserEditor.vue')
  },
  {
    path: '/chat',
    component: () => import(/* webpackChunkName: "Chat" */ '@/views/Chat')
  }
]

const router = new VueRouter({
  routes
})

// // 路由全局前置守卫
// router.beforeEach((to, from, next) => {
//   // ..需求: 登录状态下,还想去登录页面,留在原地不能跳转
//   if (store.state.token.length > 0 && to.path === '/login') {
//     next(false) // 原地留下
//   } else {
//     next() // 其他情况放行
//   }
// })

export default router
